﻿using Hhmocon.Mes.Application;
using Hhmocon.Mes.Application.Base;
using Hhmocon.Mes.Application.Energy;
using Hhmocon.Mes.Application.Response;
using Hhmocon.Mes.Repository.Domain;
using Hhmocon.Mes.Repository.Response;
using Hhmocon.Mes.Util;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace Hhmocon.Mes.WebApi.Controllers.Base
{
    [Route("api/[controller]/[action]")]
    [EnableCors(PolicyName = "_hhmoconmesAllowSpecificOrigins")]
    [ApiController]
    public class CarbonEmissionController : ControllerBase
    {
        private EnergyConsumptionApp _app;
        private PikachuApp _pikachuApp;

        public CarbonEmissionController(EnergyConsumptionApp app, PikachuApp picachuRepository)
        {
            _app = app;
            _pikachuApp = picachuRepository;
        }

        /// <summary>
        /// 碳- 碳排放量页面
        /// </summary>
        /// <param name="EndTime"></param>
        /// /// <param name="TimeCode">年传0，月传1</param>
        /// <returns></returns>
        [HttpPost]
        public Response<GetCarbonEmissionResponseTan> GetCarbon(DateTime EndTime,int TimeCode)
        {
            var result = new Response<GetCarbonEmissionResponseTan>();
            try
            {
                result.Result = _app.GetEnergyConsumption(EndTime, TimeCode);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 碳-碳中和页面
        /// </summary>
        /// <param name="EndTime"></param>
        /// <param name="TimeCode">年0 月1</param>
        /// <returns></returns>
        [HttpPost]
        public Response<GetCarbonEmissionResponseTanZhongHe> GetCarbonMidium(DateTime EndTime, int TimeCode)
        {
            var result = new Response<GetCarbonEmissionResponseTanZhongHe>();
            try
            {
                result.Result = _app.GetCarbonMedium(EndTime, TimeCode);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }


        [HttpPost]
        public Response<PageData> GetList(PageReq req)
        {
            var result = new Response<PageData>();
            try
            {
                PageData pd = new PageData();
                long lcount = 0;
                pd.Data = _pikachuApp.GetList<energy_carbon_emission>(req, ref lcount);
                pd.Total = lcount;
                result.Result = pd;
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 根据设ID删除elec_distribution_meter数据,可批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string[]> Delete(string[] ids)
        {
            var result = new Response<string[]>();
            try
            {
                result.Result = ids;

                if (!_pikachuApp.Delete_Mask<elec_distribution_meter>(ids))
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "操作失败！";
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 新建energy_carbon_emission数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string> Create(energy_carbon_emission obj)
        {
            var result = new Response<string>();
            try
            {
                energy_carbon_emission data = _app.InsertCarbon(obj);
                if (data != null)
                {
                    result.Result = data.energy_consumption_id;
                }
                else
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "数据写入失败！";
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 根据设ID删除energy_carbon_emission数据,可批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public Response<string[]> DeleteCarbon(string[] ids)
        {
            var result = new Response<string[]>();
            try
            {
                result.Result = ids;

                if (!_pikachuApp.Delete_Mask<energy_carbon_emission>(ids))
                {
                    //更新失败
                    result.Code = 100;
                    result.Message = "操作失败！";
                }
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }
    }
}
